package org.example.controller;

import org.example.annotation.CheckPermission;
import org.example.pojo.domain.NotificationSubscription;
import org.example.result.Result;
import org.example.service.NotificationSubscriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/subscriptions")
public class NotificationSubscriptionController {

    @Autowired
    private NotificationSubscriptionService subscriptionService;

    // 获取某个用户的所有订阅记录
    @CheckPermission({"visitor","admin"})
    @GetMapping("/user/{userId}")
    public Result getSubscriptionsByUser(@PathVariable Integer userId) {
        List<NotificationSubscription> subscriptions = subscriptionService.getSubscriptionsByUser(userId);
        return Result.success("用户("+userId+")的所有订阅记录",subscriptions);
    }

    // 用户订阅通知
    @CheckPermission({"visitor"})
    @PostMapping("/subscribe")
    public Result subscribe(@RequestParam Integer userId, @RequestParam String notificationType) {
        subscriptionService.subscribe(userId, notificationType);
        return Result.success("订阅成功！");
    }

    // 用户取消订阅
    @CheckPermission({"visitor"})
    @DeleteMapping("/unsubscribe/{subscriptionId}")
    public Result unsubscribe(@PathVariable Integer subscriptionId) {
        subscriptionService.unsubscribe(subscriptionId);
        return Result.success("取消订阅成功！");
    }

    // 根据通知类型获取所有订阅记录
    @CheckPermission({"visitor","admin"})
    @GetMapping("/type/{notificationType}")
    public Result getSubscriptionsByType(@PathVariable String notificationType) {
        List<NotificationSubscription> subscriptions = subscriptionService.getSubscriptionsByType(notificationType);
        return Result.success("通知类型("+notificationType+")获取所有订阅记录",subscriptions);
    }
}
